// CSimpleShlExt.cpp : Implementation of CCSimpleShlExt

#include "stdafx.h"
#include <atlconv.h>
#include <winerror.h>
#include "CSimpleShlExt.h"


STDMETHODIMP CCSimpleShlExt::Initialize(LPCITEMIDLIST pidlFolder,
									LPDATAOBJECT pDataObj,
									HKEY hProgID)
{
	FORMATETC fmt = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
	STGMEDIUM stg = {TYMED_HGLOBAL};
	HDROP hDrop;

	if (FAILED(pDataObj->GetData(&fmt, &stg)))
	{
		return E_INVALIDARG;
	}

	hDrop = (HDROP)GlobalLock(stg.hGlobal);

	if (NULL == hDrop)
	{
		return E_INVALIDARG;
	}

	UINT uNumFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
	HRESULT hr = S_OK;

	if (0 == uNumFiles)
	{
		GlobalUnlock(stg.hGlobal);
		ReleaseStgMedium(&stg);
		return E_INVALIDARG;
	}

	if (0 == DragQueryFile(hDrop, 0, m_szFile, MAX_PATH))
	{
		hr = E_INVALIDARG;
	}

	GlobalUnlock(stg.hGlobal);
	ReleaseStgMedium(&stg);
	return hr;
}

HRESULT CCSimpleShlExt::QueryContextMenu(HMENU hmenu, UINT uMenuIndex,
										 UINT uidFirstCmd, UINT uidLastCmd,
										 UINT uFlags)
{
	if (uFlags & CMF_DEFAULTONLY)
	{
		return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
	}
	InsertMenu(hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd, 
		_T("SimpleShlExt Text Item"));
	return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 1);
}

HRESULT CCSimpleShlExt::GetCommandString(UINT idCmd, UINT uFlags, UINT * pwReserved,
										 LPSTR pszName, UINT cchMax)
{
	USES_CONVERSION;
	if (0 != idCmd)
	{
		return E_INVALIDARG;
	}

	if (uFlags & GCS_HELPTEXT)
	{
		LPCTSTR szText = _T("This is the simple shell extension's help");
		if (uFlags & GCS_UNICODE)
		{
			lstrcpynW((LPWSTR)pszName, T2CW(szText), cchMax);
		}
		else
		{
			lstrcpynA(pszName, T2CA(szText), cchMax);
		}
		return S_OK;
	}
	return E_INVALIDARG;
}

HRESULT CCSimpleShlExt::InvokeCommand(LPCMINVOKECOMMANDINFO pCmdInfo)
{
	if (0 != HIWORD(pCmdInfo->lpVerb))
	{
		return E_INVALIDARG;
	}
	switch (LOWORD(pCmdInfo->lpVerb))
	{
	case 0:
		{
			TCHAR szMsg[MAX_PATH+32];
			wsprintf(szMsg, _T("The selected files was:\n\n%s"), m_szFile);
			MessageBox(pCmdInfo->hwnd, szMsg, _T("SimpleShlExt"),
				MB_ICONINFORMATION);
			return S_OK;
		}
		break;
	default:
		return E_INVALIDARG;
		break;
	}
}


// CCSimpleShlExt

